import ca.newcommerce.youtube.events.VideoFeedEvent;
import ca.newcommerce.youtube.feeds.VideoFeed;
import ca.newcommerce.youtube.webservice.YouTubeFeedClient;

import components.MyPopUpWindow_About;
import components.MyPopUpWindow_ContactUs;
import components.MyPopUpWindow_Desktop;

import events.DoPlaylistSearch;
import events.DoSearchEvent;
import events.DoSearchRelatedEvent;
import events.PlaySongEvent;
import events.PlayVideoEvent;
import events.SearchResult;

import flash.events.Event;

import mx.charts.AreaChart;
import mx.collections.*;
import mx.controls.Alert;
import mx.controls.PopUpButton;
import mx.core.IFlexDisplayObject;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.events.FlexMouseEvent;
import mx.managers.PopUpManager;

import skins.appSkin;

import spark.components.TitleWindow;

import components.propertiesWin;

include "appNewUserWindow.as";
include "appGetServerInfo.as";
include "appHandlePlaylistSongs.as";

public static var MAX_RESULTS:Number = 10;

public var pyAMFserverUrl:String;

private var client:YouTubeFeedClient = YouTubeFeedClient.getInstance();
// FIXME :: use hashmap instead, in order to be able to handle multiple requests.
private var requestId:Number;
private var searchedText:String;			

public function initData():void {
	client.addEventListener(VideoFeedEvent.VIDEO_DATA_RECEIVED, handleVideoDataResult);
	client.addEventListener(VideoFeedEvent.RELATED_VIDEOS_DATA_RECEIVED, handleRelatedVideos);
	this.addEventListener(DoSearchEvent.DO_SEARCH_EVENT,doSearchEventHandler);
	this.addEventListener(PlayVideoEvent.PLAY_VIDEO_EVENT,handlePlayVideoEvent);
	this.addEventListener(DoPlaylistSearch.DO_PLAYLIST_SEARCH, doPlayListSearchEventHandler);
	this.addEventListener(PlaySongEvent.PLAY_SONG_EVENT, handlePlaySongEvent);
}

private function handlePlaySongEvent(playSongEvent:PlaySongEvent):void{
	player.handlePlaySongEvent(playSongEvent);
	var songData:Object=playSongEvent.songData;
	var videoId:String=songData.id;
	if(videoId!=null /* and not null*/ && videoId.length>0){
		var relatedVideosEvent:DoSearchRelatedEvent = new DoSearchRelatedEvent(videoId);
		handleSearchRelatedEvent(relatedVideosEvent);
	}
}

private var relatedVideosRequestId:int=-1;

private function handleSearchRelatedEvent(relatedVideosEvent:DoSearchRelatedEvent):void
{
	relatedVideosRequestId = client.getRelatedVideos(relatedVideosEvent.videoID,relatedVideosEvent.startIndex,MAX_RESULTS);
}

private function handlePlayVideoEvent(playVideoEvent:PlayVideoEvent):void{
	player.handlePlayVideoEvent(playVideoEvent);
}

private function handleRelatedVideos(evt:VideoFeedEvent):void{
	if(relatedVideosRequestId == evt.requestId)
	{
		if(evt.feed != null && evt.feed.totalResults > 0)
		{
			var searchResult:SearchResult = new SearchResult(searchedText,evt.feed,true);
			searchAndSuggestionsTabs.handleSuggestionsResult(searchResult);
		} else {
			trace("this request:"+evt.requestId+" is empty");
		}
	}
}

public function handleVideoDataResult(evt:VideoFeedEvent):void
{
	// Check to see if this request's response is the right one
	if(requestId == evt.requestId)
	{
		if(evt.feed != null)
		{
			var searchResult:SearchResult = new SearchResult(searchedText,evt.feed);
			searchAndSuggestionsTabs.handleVideoDataResult(searchResult);
		} else {
			trace("this request:"+evt.requestId+" is null");
		}
	}
	else
	{
		trace("this request:"+evt.requestId+" is not ours. we'll wait for the next one");
	}
}

protected function doSearchEventHandler(event:DoSearchEvent):void
{
	this.requestId = client.getVideos(event.searchedText,"",null,null,null,null,YouTubeFeedClient.ORDER_BY_RELEVANCE,YouTubeFeedClient.RACY_EXCLUDE,event.startIndex,MAX_RESULTS);
	this.searchedText = event.searchedText;
}

protected function doPlayListSearchEventHandler(event:DoPlaylistSearch):void {
	this.curr_plylist.filter(event.searchedText.toLowerCase());
	
}

//--------------------------HelpButtons----------------------------------------------
private var popup_ContactUs:MyPopUpWindow_ContactUs;
private var popup_About:MyPopUpWindow_About;
private var popup_Desktop:MyPopUpWindow_Desktop;


import flash.net.URLRequest;
import flash.net.navigateToURL;

private function openPopUpWindow_Help(evt:MouseEvent):void {
	navigateToURL(new URLRequest(this.helpUrl), "_blank");
}
private function openPopUpWindow_ContactUs(evt:MouseEvent):void {
	popup_ContactUs = PopUpManager.createPopUp(this,MyPopUpWindow_ContactUs,true) as MyPopUpWindow_ContactUs;
	PopUpManager.centerPopUp(popup_ContactUs);
	popup_ContactUs.addEventListener(Event.CLOSE, closePopUpWindow_ContactUs);
	popup_ContactUs.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, closePopUpWindow_ContactUs);
}
private function openPopUpWindow_About(evt:MouseEvent):void {
	popup_About = PopUpManager.createPopUp(this,MyPopUpWindow_About,true) as MyPopUpWindow_About;
	PopUpManager.centerPopUp(popup_About);
	popup_About.addEventListener(Event.CLOSE, closePopUpWindow_About);
	popup_About.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, closePopUpWindow_About);
}

private function openPopUpWindow_Desktop(evt:MouseEvent):void {
	popup_Desktop = PopUpManager.createPopUp(this,MyPopUpWindow_Desktop,true) as MyPopUpWindow_Desktop;
	PopUpManager.centerPopUp(popup_Desktop);
	popup_Desktop.addEventListener(Event.CLOSE, closePopUpWindow_Desktop);
	popup_Desktop.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, closePopUpWindow_Desktop);
}


private function closePopUpWindow_ContactUs(evt:Event):void {
	PopUpManager.removePopUp(popup_ContactUs);
}
private function closePopUpWindow_About(evt:Event):void {
	PopUpManager.removePopUp(popup_About);
}
private function closePopUpWindow_Desktop(evt:Event):void {
	PopUpManager.removePopUp(popup_Desktop);
}

// ---------------- properties -----------------
private var poropWin:propertiesWin;
private function openPropertiesWindow(propObj:Object):void
{
	poropWin = PopUpManager.createPopUp(this,propertiesWin,true) as propertiesWin;
	PopUpManager.centerPopUp(poropWin);
	poropWin.addEventListener(Event.CLOSE, closePopUpWindow_prop);
	poropWin.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, closePopUpWindow_prop);
	poropWin.setProperties(propObj);
}

private function closePopUpWindow_prop(evt:Event):void {
	PopUpManager.removePopUp(poropWin);
}